package com.xiaoyu.lanling.feature.moment.viewholder.publish

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.facebook.drawee.view.SimpleDraweeView
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.event.moment.publish.DeletePictureEvent
import com.xiaoyu.lanling.event.moment.publish.PreviewPictureClickEvent
import com.xiaoyu.lanling.feature.moment.datamodel.publish.MediaImageItem
import com.xiaoyu.lanling.media.image.ImageViewUtil

class MediaImageViewHolder : MediaBaseViewHolder<MediaImageItem>() {

    private var mView: View? = null
    private var mImageView: SimpleDraweeView? = null
    private var mDeleteView: ImageView? = null

    override fun createView(layoutInflater: LayoutInflater, parent: ViewGroup): View? {
        mView = layoutInflater.inflate(R.layout.moment_publish_image_item, parent, false)
        mImageView = mView?.findViewById(R.id.drawee_view)
        mDeleteView = mView?.findViewById(R.id.delete_view)

        mView?.setOnClickDebounceListener(onClickHandler)
        mDeleteView?.setOnClickDebounceListener(deleteOnClickHandler)

        return mView
    }

    override fun showData(position: Int, item: MediaImageItem) {
        ImageViewUtil.loadImage(mImageView, item.imageLoadParam)
        mView?.setViewTag(item)
        mDeleteView?.setViewTag(item)
    }

    private val onClickHandler = View.OnClickListener { v ->
        val item = v.getViewTag<MediaImageItem>()
                ?: return@OnClickListener
        PreviewPictureClickEvent(item).post()
    }

    private val deleteOnClickHandler = View.OnClickListener { v ->
        val item = v.getViewTag<MediaImageItem>()
                ?: return@OnClickListener

        DeletePictureEvent(item.position, item.path).post()
    }
}
